package com.jqr.activity.myrecyclerviewdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.jqr.activity.myrecyclerviewdemo.adapters.GridViewAdaper;
import com.jqr.activity.myrecyclerviewdemo.adapters.ListViewRecycleViewAdapter;
import com.jqr.activity.myrecyclerviewdemo.adapters.StaggerAdaper;
import com.jqr.activity.myrecyclerviewdemo.model.ItemModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static  String TAG="MainActivity";

    private RecyclerView recyclerView;

    private List<ItemModel> itemModels;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView=findViewById(R.id.rvListViewVerticle);
        //准备数据
        initData();
        // 适配器

    }
    //初始化数据
    private void initData() {
        //List<T>  Adapter  setAdapter x

        itemModels=new ArrayList<>();
        for (int i=0;i<20;i++){

            ItemModel itemModel=new ItemModel();
            int j=i%4;
            switch (j){
                case 0 :
                    itemModel.setIcon(R.drawable.a);
                    break;
                case 1 :
                    itemModel.setIcon(R.drawable.c);
                    break;
                case 2 :
                    itemModel.setIcon(R.drawable.d);
                    break;
                default:
                    itemModel.setIcon(R.drawable.weixin);
            }


            itemModel.setTitle("recycleview "+i);

            itemModels.add(itemModel);
        }
        //设置布局管理器
        showListViewVerticle(true,false);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        int itemId=item.getItemId();
        switch (itemId){
            case R.id.list_view_verticle:
                Log.d(TAG, "list_view_verticle");
                showListViewVerticle(true,false);
                break;
            case R.id.list_view_verticle_reverse:
                Log.d(TAG, "list_view_verticle_reverse");
                showListViewVerticle(true,true);
                break;
            case R.id.list_view_horizontal_reverse:
                Log.d(TAG, "list_view_horizontal_reverse");
                showListViewVerticle(false,false);
                break;
            case R.id.list_view_horizontal:
                Log.d(TAG, "list_view_horizontal");
                showListViewVerticle(false,true);
                break;
            case R.id.grid_view_verticle:
                showGrid(false,false);
                Log.d(TAG, "grid_view_verticle");
                break;
            case R.id.grid_view_verticle_reverse:
                showGrid(false,true);
                Log.d(TAG, "grid_view_verticle_reverse");
                break;
            case R.id.grid_view_horizontal:
                showGrid(true,false);
                Log.d(TAG, "grid_view_horizontal");
                break;
            case R.id.grid_view_horizontal_reverse:
                showGrid(true,true);
                Log.d(TAG, "grid_view_horizontal_reverse");
                break;
            case R.id.water_view_verticle:
                showWater(true,false);
                Log.d(TAG, "water_view_verticle");
                break;
            case R.id.water_view_verticle_reverse:
                showWater(true,true);
                Log.d(TAG, "water_view_verticle_reverse");
                break;
            case R.id.water_view_horizontal:
                showWater(false,false);
                Log.d(TAG, "water_view_horizontal");
                break;
            case R.id.water_view_horizontal_reverse:
                showWater(false,true);
                Log.d(TAG, "water_view_horizontal_reverse");
                break;
            default:
                Log.d(TAG, "default");

        }
        return super.onOptionsItemSelected(item);
    }

    private void showWater(boolean isVertical,boolean isReverse) {
        StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,isVertical?StaggeredGridLayoutManager.VERTICAL:StaggeredGridLayoutManager.HORIZONTAL);
        layoutManager.setReverseLayout(isReverse);
        recyclerView.setLayoutManager(layoutManager);

        StaggerAdaper staggerAdaper=new StaggerAdaper(itemModels);

        recyclerView.setAdapter(staggerAdaper);
    }

    private void showGrid(boolean isVertical,boolean isReverse) {

        GridLayoutManager layoutManager=new GridLayoutManager(this,4);
        layoutManager.setOrientation(isVertical? LinearLayoutManager.HORIZONTAL:LinearLayoutManager.VERTICAL);
        layoutManager.setReverseLayout(isReverse);

        GridViewAdaper gridViewAdaper=new GridViewAdaper(itemModels);

        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setAdapter(gridViewAdaper);
        }

    private void showListViewVerticle(boolean isVertical,boolean isReverse) {
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(isVertical? LinearLayoutManager.VERTICAL:LinearLayoutManager.HORIZONTAL);

        layoutManager.setReverseLayout(isReverse);
        recyclerView.setLayoutManager(layoutManager);

        ListViewRecycleViewAdapter adapter=new ListViewRecycleViewAdapter(itemModels);


        recyclerView.setAdapter(adapter);

    }
}